#!/usr/bin/groovy
// -*- mode: groovy -*-

/* Jenkins pipeline for Windows AMD64 target */

import groovy.transform.Field

@Field
def commit_id   // necessary to pass a variable from one stage to another

pipeline {
  agent none

  // Setup common job properties
  options {
    timestamps()
    timeout(time: 240, unit: 'MINUTES')
    buildDiscarder(logRotator(numToKeepStr: '10'))
    preserveStashes()
  }

  // Build stages
  stages {
    stage('Jenkins Win64: Initialize') {
      agent { label 'job_initializer' }
      steps {
        script {
          def buildNumber = env.BUILD_NUMBER as int
          if (buildNumber > 1) milestone(buildNumber - 1)
          milestone(buildNumber)
          checkoutSrcs()
          commit_id = "${GIT_COMMIT}"
        }
        sh 'python3 tests/jenkins_get_approval.py'
        stash name: 'srcs'
      }
    }
    stage('Jenkins Win64: Build') {
      agent none
      steps {
        script {
          parallel ([
            'build-win64-cuda10.1': { BuildWin64() },
            'build-rpkg-win64-cuda10.1': { BuildRPackageWithCUDAWin64() }
          ])
        }
      }
    }
    stage('Jenkins Win64: Test') {
      agent none
      steps {
        script {
          parallel ([
            'test-win64-cuda10.1': { TestWin64() },
          ])
        }
      }
    }
  }
}

// check out source code from git
def checkoutSrcs() {
  retry(5) {
    try {
      timeout(time: 2, unit: 'MINUTES') {
        checkout scm
        sh 'git submodule update --init'
      }
    } catch (exc) {
      deleteDir()
      error "Failed to fetch source codes"
    }
  }
}

def BuildWin64() {
  node('win64 && cuda10_unified') {
    deleteDir()
    unstash name: 'srcs'
    echo "Building XGBoost for Windows AMD64 target..."
    bat "nvcc --version"
    def arch_flag = ""
    if (env.BRANCH_NAME != 'master' && !(env.BRANCH_NAME.startsWith('release'))) {
      arch_flag = "-DGPU_COMPUTE_VER=75"
    }
    bat """
    mkdir build
    cd build
    cmake .. -G"Visual Studio 15 2017 Win64" -DUSE_CUDA=ON -DCMAKE_VERBOSE_MAKEFILE=ON -DGOOGLE_TEST=ON -DUSE_DMLC_GTEST=ON ${arch_flag} -DCMAKE_UNITY_BUILD=ON
    """
    bat """
    cd build
    "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\MSBuild\\15.0\\Bin\\MSBuild.exe" xgboost.sln /m /p:Configuration=Release /nodeReuse:false
    """
    bat """
    cd python-package
    conda activate && python setup.py bdist_wheel --universal && for /R %%i in (dist\\*.whl) DO python ../tests/ci_build/rename_whl.py "%%i" ${commit_id} win_amd64
    """
    echo "Insert vcomp140.dll (OpenMP runtime) into the wheel..."
    bat """
    cd python-package\\dist
    COPY /B ..\\..\\tests\\ci_build\\insert_vcomp140.py
    conda activate && python insert_vcomp140.py *.whl
    """
    echo 'Stashing Python wheel...'
    stash name: 'xgboost_whl', includes: 'python-package/dist/*.whl'
    if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME.startsWith('release')) {
      echo 'Uploading Python wheel...'
      path = ("${BRANCH_NAME}" == 'master') ? '' : "${BRANCH_NAME}/"
      s3Upload bucket: 'xgboost-nightly-builds', path: path, acl: 'PublicRead', workingDir: 'python-package/dist', includePathPattern:'**/*.whl'
    }
    echo 'Stashing C++ test executable (testxgboost)...'
    stash name: 'xgboost_cpp_tests', includes: 'build/testxgboost.exe'
    stash name: 'xgboost_cli', includes: 'xgboost.exe'
    deleteDir()
  }
}

def BuildRPackageWithCUDAWin64() {
  node('win64 && cuda10_unified') {
    deleteDir()
    unstash name: 'srcs'
    bat "nvcc --version"
    if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME.startsWith('release')) {
      bat """
      bash tests/ci_build/build_r_pkg_with_cuda_win64.sh ${commit_id}
      """
      echo 'Uploading R tarball...'
      path = ("${BRANCH_NAME}" == 'master') ? '' : "${BRANCH_NAME}/"
      s3Upload bucket: 'xgboost-nightly-builds', path: path, acl: 'PublicRead', includePathPattern:'xgboost_r_gpu_win64_*.tar.gz'
    }
    deleteDir()
  }
}

def TestWin64() {
  node('win64 && cuda10_unified') {
    deleteDir()
    unstash name: 'srcs'
    unstash name: 'xgboost_whl'
    unstash name: 'xgboost_cli'
    unstash name: 'xgboost_cpp_tests'
    echo "Test Win64"
    bat "nvcc --version"
    echo "Running C++ tests..."
    bat "build\\testxgboost.exe"
    echo "Installing Python dependencies..."
    def env_name = 'win64_' + UUID.randomUUID().toString().replaceAll('-', '')
    bat "conda activate && mamba env create -n ${env_name} --file=tests/ci_build/conda_env/win64_test.yml"
    echo "Installing Python wheel..."
    bat """
    conda activate ${env_name} && for /R %%i in (python-package\\dist\\*.whl) DO python -m pip install "%%i"
    """
    echo "Running Python tests..."
    bat "conda activate ${env_name} && python -m pytest -v -s -rxXs --fulltrace tests\\python"
    bat """
    conda activate ${env_name} && python -m pytest -v -s -rxXs --fulltrace -m "(not slow) and (not mgpu)" tests\\python-gpu
    """
    bat "conda env remove --name ${env_name}"
    deleteDir()
  }
}
